package net.cmkj.mine.util;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Arguments {

    public static Object[] params = null;
    public static int[] types = null;

    public static String getQueryArguments(Map<String, Object> args) {

        return "";
    }

    public static String makeQueryArguments(Map<String, Object> args) {
        StringBuffer sql = new StringBuffer();
        StringBuffer ord = new StringBuffer();
        StringBuffer limit = new StringBuffer();
        String val = " = ?";
        params = null;
        types = null;
        List<Object> params = new ArrayList<Object>();
        List<Integer> type = new ArrayList<Integer>();
        try {
            if (args != null) {
                for (Entry<String, Object> entry : args.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    if ("sort".equalsIgnoreCase(key)) {
                        if (ord.length() == 0) {
                            ord.append(" order by "+ value);
                        } else {
                            ord.append(", "+value);
                        }
                    } else if ("order".equalsIgnoreCase(key)) {
                        if (ord.length() != 0) {
                            ord.append(" "+value);
                        }
                    } else {
                        String v = value.toString();
                        if (v.trim().startsWith(">") || v.trim().startsWith(">=") || v.trim().startsWith("<") || v.trim().startsWith("<=")) {

                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String makeDeleteArguments(Map<String, Object> args) {
        StringBuffer sql = new StringBuffer();
        String val = "";
        params = null;
        types = null;
        List<Object> param = new ArrayList<Object>();
        List<Integer> type = new ArrayList<Integer>();
        try {
            if (args != null && args.size() > 0)
                for (Entry<String, Object> entry : args.entrySet()) {
                    if (entry.getKey().equals("sort") || entry.getKey().equals("order") || entry.getKey().equals("currentPage") || entry.getKey().equals("pageSize"))
                        continue;
                    if (entry.getKey().contains("id") && entry.getValue().toString().contains(",")) {
                        if (sql.length() == 0)
                            sql.append(" where " + entry.getKey() + " in (" + entry.getValue().toString() + ")");
                        else
                            sql.append(" and " + entry.getKey() + " in (" + entry.getValue().toString() + ")");
                    } else {
                        if (entry.getKey().contains("id") && !entry.getValue().toString().contains(",")) {
                            param.add(entry.getValue().toString());
                            type.add(Types.BIGINT);
                        } else if (entry.getValue().toString().contains(",")) {
                            param.add(entry.getValue().toString().replaceAll(",", "','"));
                            type.add(Types.VARCHAR);
                        } else {
                            param.add(entry.getValue());
                            type.add(Types.VARCHAR);
                        }
                        if (entry.getValue().toString().contains("%"))
                            val = " like ?";
                        else if (entry.getValue().toString().contains(","))
                            val = " in (?)";
                        else
                            val = " =?";
                        if (sql.length() == 0)
                            sql.append(" where " + entry.getKey() + val);
                        else
                            sql.append(" and " + entry.getKey() + val);
                    }
                }
            types = new int[type.size()];
            params = param.toArray();
            for (int i = 0; i < type.size(); i++) {
                types[i] = type.get(i);
            }
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return sql.toString();
    }

    public static String makeUpdateArguments(Map<String, Object> args) {
        StringBuffer sql = new StringBuffer();
        StringBuffer val = new StringBuffer();
        params = null;
        types = null;
        List<Object> param = new ArrayList<Object>();
        List<Integer> type = new ArrayList<Integer>();
        String updateKey = "updateKey,";
        try {
            if (args != null && args.size() > 0) {
                updateKey += args.get("updateKey").toString();
                for (Entry<String, Object> entry : args.entrySet()) {
                    if (entry.getKey().equals("sort") || entry.getKey().equals("order") || entry.getKey().equals("currentPage") || entry.getKey().equals("pageSize"))
                        continue;
                    if (updateKey.contains(entry.getKey()))
                        continue;
                    if (sql.length() == 0)
                        sql.append(" " + entry.getKey() + "=?");
                    else
                        sql.append(", " + entry.getKey() + "=?");
                    if (entry.getKey().contains("id")) {
                        param.add(entry.getValue().toString());
                        type.add(Types.BIGINT);
                    } else {
                        param.add(entry.getValue().toString());
                        type.add(Types.VARCHAR);
                    }
                }
                updateKey = updateKey.replaceAll("updateKey,", "");
                for (Entry<String, Object> entry : args.entrySet()) {
                    if (!updateKey.contains(entry.getKey()))
                        continue;

                    if (entry.getKey().contains("id") && entry.getValue().toString().contains(",")) {
                        if (val.length() == 0)
                            val.append(" where " + entry.getKey() + " in (" + entry.getValue().toString() + ")");
                        else
                            val.append(" and " + entry.getKey() + " in (" + entry.getValue().toString() + ")");
                    } else {

                        if (val.length() == 0)
                            val.append(" where " + entry.getKey() + "=?");
                        else
                            val.append(" and " + entry.getKey() + "=?");
                        if (entry.getKey().contains("id")) {
                            param.add(entry.getValue().toString());
                            type.add(Types.BIGINT);
                        } else {
                            param.add(entry.getValue().toString());
                            type.add(Types.VARCHAR);
                        }
                    }
                }
            }
            types = new int[type.size()];
            params = param.toArray();
            for (int i = 0; i < type.size(); i++)
                types[i] = type.get(i);

        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }

        return sql.toString() + val.toString();
    }

    public static String makeInsertArguments(Map<String, Object> args, Object[] params, int[] types) {
        StringBuffer sql = new StringBuffer();
        StringBuffer val = new StringBuffer();
        int i = 0;
        try {
            if (args != null && args.size() > 0) {
                for (Entry<String, Object> entry : args.entrySet()) {
                    if (entry.getKey().equals("sort") || entry.getKey().equals("order") || entry.getKey().equals("currentPage") || entry.getKey().equals("pageSize"))
                        continue;
                    if (sql.length() == 0) {
                        sql.append(" (" + entry.getKey());
                        val.append(" values(?");
                    } else {
                        sql.append("," + entry.getKey());
                        val.append(",?");
                    }
                    if (entry.getKey().contains("id")) {
                        params[i] = Long.parseLong(entry.getValue().toString());
                        types[i] = Types.BIGINT;
                    } else {
                        params[i] = entry.getValue();
                        types[i] = Types.VARCHAR;
                    }
                    i++;
                }
                sql.append(")");
                val.append(")");
            }
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }

        return sql.toString() + val.toString();
    }

    public static String convertErrorCode(int code) {
        String s = "操作成功";
        if (code > 0)
            s += ",数据条数：" + code;
        else
            switch (code) {
                case -1:
                    s = "系统错误";
                    break;
                case -2:
                    s = "缺少条件参数";
                    break;
                case -3:
                    s = "此信息已存在";
                    break;
                case -4:
                    s = "更新操作必须设置updateKey对应的KEY字段列表字符串逗号分隔";
                    break;
                case -5:
                    s = "发送邮件失败";
                    break;
                case -6:
                    s = "没有此季度的基础数据，不能建立此季度报告";
                    break;
                case -7:
                    s = "没有符合条件的报告被添加，请检查是否已经添加过。";
                    break;
                default:
                    s = "操作失败, 代码：" + code;
                    break;
            }
        return s;
    }

}
